/*
 * HubInfoConnection.java
 *
 * Created on June 25, 2006, 4:56 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.conn;

import libjdc.dc.client.HubInfo;
import libjdc.dc.client.User;
import libjdc.dc.protocol.Encripter;
import libjdc.dc.protocol.Protocol;
import hubsniffer.*;
import libjdc.dc.log.Log;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;

/**
 *
 * @author root
 */
public class HubInfoConnection extends HubConnection{
    
    /** Creates a new instance of HubInfoConnection */
    public HubInfoConnection(DCHub hub,User me) {
        super(hub,me);
    }
    
    protected void processHubMessage(String message) throws IOException {
        try {
            Log.out.println(message);
            if (message.startsWith(Protocol.DC_S_MESSAGE_LOCK))
                handlerLock(message);
            else if (message.startsWith(Protocol.DC_S_MESSAGE_HUBINFO))
                handlerHubInfo(message);
            /*else if (message.startsWith(Protocol.DC_S_MESSAGE_GETPASS))
                super.handlerGetPass();*/
            /*else
                super.processHubMessage(message);*/
        }catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(Log.err);
        }
    }
    
    protected void handlerLock(String lock) throws ParseException, UnsupportedEncodingException, IOException{
        Object[] obj = Protocol.LOCK_PARSER.parse(lock);

        this.sendBotInfo(this.user.getUserInfo().getDescription(),new Encripter(obj[0].toString()).getKey());
        super.sendValidateNick();
        //sendVersion();
        //super.sendGetNickList();
    }
    
    protected void handlerHubInfo(String hubInfo) throws ParseException{
        Object[] obj = Protocol.HUBINFO_PARSER.parse(hubInfo);
        
        /*int i=0;
        hub.setHubName(obj[i++].toString());
        hub.setHubAddress(obj[i++].toString());
        hub.setHubDescription(obj[i++].toString());
        hub.setHubMaxUsers(obj[i++].toString());
        hub.setHubMinShare(obj[i++].toString());
        hub.setHubMinSlots(obj[i++].toString());
        hub.setHubMaxHubs(obj[i++].toString());*/
        
        String hub_info_name = (obj[0].toString().equals(""))? null:obj[0]+"";
        
        String hub_info_address;
        String hub_info_port;
        if(obj[1].toString().equals("")){
            hub_info_address = null;
            hub_info_port =null;
        }else{
            if((obj[1]+"").indexOf(":") > 0){
                hub_info_address = (obj[1]+"").substring(0,(obj[1]+"").indexOf(":"));
                hub_info_port = (obj[1]+"").substring((obj[1]+"").indexOf(":")+1);
            }else{
                hub_info_address =(obj[1]+"");
                hub_info_port = "411";
            }
        }
        String hub_info_description = (obj[2].toString().equals(""))? null:obj[2]+"";
        Long hub_info_max_users = (obj[3].toString().equals(""))? null:new Long(obj[3]+"");
        Long hub_info_min_share = (obj[4].toString().equals(""))? null:new Long(obj[4]+"");
        Integer hub_info_min_slots = (obj[5].toString().equals(""))? null:new Integer(obj[5]+"");
        Integer hub_info_max_hubs = (obj[6].toString().equals(""))? null:new Integer(obj[6]+"");
        String hub_info_hub_type = (obj[7].toString().equals(""))? null:obj[7]+"";
        String hub_info_hubowner = (obj[8].toString().equals(""))? null:obj[8]+"";
        
        
        hub.setHubInfo(
                new HubInfo(hub_info_name,hub_info_address,hub_info_port,hub_info_description,hub_info_max_users,hub_info_min_share,hub_info_min_slots,hub_info_max_hubs,hub_info_hub_type,hub_info_hubowner)
                );
        
        try {
            super.disconnect();
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void sendBotInfo(String description,String key) throws UnsupportedEncodingException, IOException{
        Object[] obj = { description };

        //sendRaw( "$Supports BotINFO|"+Protocol.BOTINFO_PARSER.format( obj )+"|$ValidateNick [PT]|" );
        super.sendRaw( "$Supports BotINFO|$Key "+key+"|$BotINFO "+description+"|" );
    }
    
    
}
